package com.itheima.service.impl;

import com.itheima.dao.RoleMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import com.itheima.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Create By Awen on 2021/8/22
 *
 * @author Awen
 */
public class UserServiceImpl implements UserService {
    @Override
    public User login(User user) {
        SqlSession session = SqlSessionUtil.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        // 对用户输入的密码使用MD5加密.对比加密后的密文
        String md5Password = MD5Utils.encode(user.getPassword());
        user.setPassword(md5Password);
        System.out.println("加密后的数据: "+user);
        User u = mapper.login(user);
        session.close();
        return u;
    }

    @Override
    public PageBean<User> queryUsersByPage(int currentPage, int pageSize) {
        PageBean<User> pb = new PageBean<>();
        SqlSession session = SqlSessionUtil.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        //总条数
        int totalCount = mapper.queryUsersCount();
        //起始索引
        int startIndex = (currentPage - 1) * pageSize;
        //用户列表
        List<User> userList = mapper.queryUsersByPage(startIndex, pageSize);
        System.out.println("用户列表 = " + userList);
        // 遍历用户对象,查询用户对应的角色信息
        for (User user : userList) {
            List<Role> list = roleMapper.queryRoleById(user.getId());
            user.setRoleList(list);
        }

        pb.setList(userList);
        pb.setCurrentPage(currentPage);
        pb.setTotalCount(totalCount);
        pb.setPageSize(pageSize);
        session.close();
        return pb;
    }

    @Override
    public void addUser(User user) {
        SqlSession session = SqlSessionUtil.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.addUser(user);
        int userId = user.getId();
        for (String roleId : user.getRoleIds()) {
            mapper.addUserAndRole(userId,roleId);
        }
        session.close();

    }

    @Override
    public void updateUserById(User user) {
        SqlSession session = SqlSessionUtil.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        //先删除  后添加
        roleMapper.deleteRoleByUserId(user.getId());
        mapper.updateUserById(user);
        for (String roleId : user.getRoleIds()) {
            mapper.addUserAndRole(user.getId(),roleId);
        }
        session.close();
    }

    @Override
    public void deleteUserById(String id) {
        SqlSession session = SqlSessionUtil.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.deleteUserById(id);
        session.close();
    }
}
